#include "Speedboat.h"


Speedboat::Speedboat( Ogre::SceneManager* smgr, EntityWrapper* boatObj, Hydrax::Hydrax* hydrax ):
m_Smgr(smgr), m_BoatObj(boatObj), m_Hydrax(hydrax)
{
	m_buoyancy = 150;
	m_isRun = false;
	m_Speed = 80;
	m_PS = smgr->createParticleSystem("SBoatEngine", "BoatEngine");
	Ogre::SceneNode* befnode = m_BoatObj->getRigidBody()->mNode->createChildSceneNode("BoatEngineFire", Ogre::Vector3(0, 0, -15));
	befnode->attachObject(m_PS);
	m_PS->setVisible(false);
}

Speedboat::~Speedboat()
{

}

void Speedboat::Updata()
{
	Ogre::SceneNode* node =m_BoatObj->getRigidBody()->mNode; 
	Ogre::Vector3 bpos = node->getPosition();
	float ocH = m_Hydrax->getHeigth(bpos);
	float dif = ocH - bpos.y;
	btVector3 f(0,0,0);
	if (dif>0)
	{
		f.setY(m_buoyancy);
	}

	if (m_isRun)
	{
		Ogre::Vector3 direction = node->getOrientation().zAxis();
		direction *= m_Speed;
		f += btVector3(direction.x, direction.y, direction.z);
	}
	m_BoatObj->getRigidBody()->mBody->applyForce(f, btVector3(0,0,0));
}

void Speedboat::SetRun( bool setting )
{	
	Ogre::SceneNode* node = m_BoatObj->getRigidBody()->mNode;

	m_PS->setVisible(setting);
	m_isRun = setting;
}

void Speedboat::SetTransform( Ogre::Vector3 pos, Ogre::Quaternion q )
{
	m_BoatObj->ChangePhyState(pos, q);
	m_BoatObj->getRigidBody()->mNode->setPosition(pos);
	m_BoatObj->getRigidBody()->mNode->setOrientation(q);
	m_BoatObj->getRigidBody()->mBody->clearForces();
}

void Speedboat::SetVisiable( bool setting )
{
	m_BoatObj->getRigidBody()->mNode->setVisible(setting);
}

void Speedboat::SetPhyEnable( bool setting )
{
	if (setting)
		m_BoatObj->getRigidBody()->mBody->setActivationState(ACTIVE_TAG);
	else
		m_BoatObj->getRigidBody()->mBody->setActivationState(ISLAND_SLEEPING);
}

void Speedboat::Shoot( Ogre::Vector3 f )
{
	m_BoatObj->getRigidBody()->mBody->applyImpulse(btVector3(f.x, f.y, f.z), btVector3(0,0,0));
}
